package cn.yu.takeoutbackend.dishManagement.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.yu.takeoutbackend.common.Result;
import cn.yu.takeoutbackend.dishManagement.entity.*;
import cn.yu.takeoutbackend.dishManagement.service.DishCategoryService;
import cn.yu.takeoutbackend.dishManagement.service.DishFlavorService;
import cn.yu.takeoutbackend.dishManagement.service.DishService;
import cn.yu.takeoutbackend.dishManagement.service.FlavorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController()
@RequestMapping("/dishManagement")
@Slf4j
public class DishController {
    @Autowired
    @Qualifier("dishServiceImpl")
    private DishService dishService;

    @Autowired
    @Qualifier("dishFlavorServiceImpl")
    private DishFlavorService dishFlavorService;

    @Autowired
    @Qualifier("flavorServiceImpl")
    private FlavorService flavorService;

    @Autowired
    @Qualifier("dishCategoryServiceImpl")
    private DishCategoryService dishCategoryService;

    @PostMapping("/add")
    public Result addDish(@RequestBody DishDto dishDto) {
        log.info("----------------  addDish start ----------------");
        log.info("Adding dish: {}", dishDto);
        try {
            dishService.save(dishDto);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        } finally {
            log.info("----------------  addDish end ----------------");
        }
    }

    @PostMapping("/addDishFlavor")
    public Result addDishFlavor(@RequestBody DishFlavor dishFlavor) {
        log.info("----------------  addDishFlavor start ----------------");
        log.info("Adding dishFlavor: {}", dishFlavor);
        try {
            dishFlavorService.save(dishFlavor);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        } finally {
            log.info("----------------  addDishFlavor end ----------------");
        }
    }

    @PostMapping("/addFlavor")
    public Result addFlavor(@RequestBody Flavor flavor) {
        log.info("----------------  addDishFlavor start ----------------");
        log.info("Adding flavor: {}", flavor);
        try {
            flavor.setId(IdUtil.getSnowflakeNextIdStr());
            flavorService.save(flavor);
            return Result.success("添加成功");
        } catch (DuplicateKeyException e) {
            log.info("口味已存在，{}", e);
            return Result.error("失败").setResultMessage("口味已存在");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        } finally {
            log.info("----------------  addDishFlavor end ----------------");
        }
    }

    @PostMapping("/getDishList")
    public Result getDishList(@RequestBody Dish dish) {
        log.info("----------------  getDishList start ----------------");
        log.info("getDishList dish: {}", dish);
        try {
            final QueryWrapper<Dish> wrapper = new QueryWrapper<>();

            if (!StringUtils.isEmpty(dish.getCategory())) {
                wrapper.eq("category_id", dish.getCategory());
            }
            if (!StringUtils.isEmpty(dish.getName())) {
                wrapper.eq("name", dish.getName());
            }
            wrapper.eq("status", 0);
            final List<Dish> list = dishService.list(wrapper);
            final QueryWrapper<DishFlavor> flavorQueryWrapper = new QueryWrapper<>();
            final ArrayList<DishVo> result = new ArrayList<>();
            list.forEach(d -> {
                final DishVo dishVo = new DishVo();
                BeanUtil.copyProperties(d, dishVo);
//                if (ObjectUtil.isNotNull(d.getId())) {
//                    flavorQueryWrapper.eq("dish_id", d.getId());
//                    dishVo.setFlavor(dishFlavorService.getOne(flavorQueryWrapper));
//                }
                result.add(dishVo);
            });
            return Result.success("查询成功").setResultData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        } finally {
            log.info("----------------  getDishList end ----------------");
        }
    }

    @PostMapping("/getDishCategoryList")
    public Result getDishCategoryList() {
        log.info("----------------  getDishCategoryList start ----------------");
        try {
            final List<DishCategory> list = dishCategoryService.list();
            log.info("getDishCategoryList list: {}", list);
            return Result.success("查询成功").setResultData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        } finally {
            log.info("----------------  getDishCategoryList end ----------------");
        }
    }

    @PostMapping("/getFlavorList")
    public Result getFlavorList(@RequestBody Flavor flavor) {
        log.info("----------------  getDishList start ----------------");
        log.info("getFlavorList: {}", flavor);
        try {
            final QueryWrapper<Flavor> wrapper = new QueryWrapper<>();
            final List<Flavor> list = flavorService.list(wrapper);
            return Result.success("查询成功").setResultData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        } finally {
            log.info("----------------  getDishList end ----------------");
        }
    }


    @PostMapping("/getFlavorByDishId")
    public Result getFlavorByDishId(@RequestBody String dishId) {
        log.info("----------------  getFlavorByDishId start ----------------");
        try {
            log.info("getFlavorByDishId dishId: {}", dishId);
            final QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
            wrapper.eq("dish_id", dishId);
            final DishFlavor dishFlavor = dishFlavorService.getOne(wrapper);
            return Result.success("查询成功").setResultData(dishFlavor);
        } catch (Exception e) {
            log.error("getDishFlavorByDishId error: {}", e);
            return Result.error("查询失败");
        } finally {
            log.info("----------------  getFlavorByDishId end ----------------");
        }
    }

    @PostMapping("/deleteDish")
    public Result deleteDish(@RequestBody String dishId) {
        log.info("----------------  deleteDish start ----------------");
        try {
            log.info("deleteDish dishId: {}", dishId);
            final QueryWrapper<Dish> wrapper = new QueryWrapper<>();
            wrapper.eq("id", dishId);
            final boolean remove = dishService.remove(wrapper);
            if (remove) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("deleteDish error: {}", e);
            return Result.error("删除失败");
        } finally {
            log.info("----------------  deleteDish end ----------------");
        }
    }

    @PostMapping("/updateDish")
    public Result updateDish(@RequestBody Dish dish) {
        log.info("----------------  updateDish start ----------------");
        try {
            log.info("updateDish dish: {}", dish);
            final boolean update = dishService.updateById(dish);
            if (!update) {
                return Result.error("修改失败");
            }
        } catch (Exception e) {
            log.error("updateDish error: {}", e);
            return Result.error("修改失败");
        } finally {
            log.info("----------------  updateDish end ----------------");
        }
        return Result.success("成功");
    }


}
